<?php
// $Id: attach.php 428 2012-06-03 01:21:35Z tomqin@gmail.com $

defined('IN_ADMIN') or exit('Access Denied!');

class Attach_Action extends Action_Admin{

    public function init(){
        parent::init();

        //$this->waimai->set_user($this->user);
    }

    public function onTailor(){
        $width    = array_get_int($_GET, 'width', 100);  //目标尺寸
        $height   = array_get_int($_GET, 'height', 100);
        $picid    = array_get_int($_GET, 'picid');
        $picture  = array_get_string($_GET, 'picture');

        if(array_get_bool($_POST, 'dosubmit')){
            $picture = array_get_string($_POST, 'picture');
            $x   = array_get_int($_POST, 'x');
            $y   = array_get_int($_POST, 'y');
            $w   = array_get_int($_POST, 'w');
            $h   = array_get_int($_POST, 'h');

            $filename = date('Y/m/YmdHis').'_'.mt_rand(1000, 9999);

            $image = Image::createFromFile($picture);
            $image->tailor($width, $height, $x, $y, $w, $h);
            $image->saveAsJpeg(UPLOAD_DIR.$filename.'.jpg', 90);
            $this->setVar('result', $filename.'.jpg');
        }else{
            $this->setVar('result', '');
        }

        $this->setVar('picid', $picid);
        $this->setVar('width', $width);
        $this->setVar('height', $height);
        $this->setVar('picture', $picture);
        $this->render('admin/attach_tailor');
    }

    public function onThumb(){
        $picture  = array_get_string($_GET, 'picture');
        $width    = array_get_int($_GET, 'width');
        $height   = array_get_int($_GET, 'height');

        header("Location: ".thumb_url($picture, $width, $height));
    }

    public function onUpload(){
        $client = $this->params;
        if($client == 'picture'){
            $name       = array_get_string($_POST, 'name', 'picture');
            $min_width  = array_get_int($_POST, 'min_width', 8);
            $min_height = array_get_int($_POST, 'min_height', 8);
            $max_width  = array_get_int($_POST, 'max_width', 3000);
            $max_height = array_get_int($_POST, 'max_height', 3000);
            $is_tmpfile = array_get_bool($_POST, 'is_tmpfile');

            $option   = array(
                'is_tmpfile'       => $is_tmpfile,
                'max_length'       => 2048000,
                'allow_type'       => 'image',
                'check_image'      => true,
                'image_min_width'  => $min_width,
                'image_max_width'  => $max_width,
                'image_min_height' => $min_height,
                'image_max_height' => $max_height,
            );
            $data = $this->uploadFile($name, $option);

            echo json_encode($data);
        }
    }

    protected function uploadFile($file, $option){
        $upload = new Upload($file, $option);
        $result = $upload->uploadFile();

        return array(
            'file'  => isset($upload->file['file'])?$upload->file['file']:'',
            'name'  => isset($upload->file['name'])?$upload->file['name']:'',
            'type'  => isset($upload->file['type'])?$upload->file['type']:'',
            'error' => $this->getUploadError($result, $option),
        );
    }

    private function ckhtml($funcnum, $filename, $message){
        if($message){
            $filename = '';
        }
        return '<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction('.$funcnum.', "'.$filename.'", "'.$message.'");</script>';
    }

    private function getUploadError($result, array $option){
        $type = ($option['check_image'] || $option['allow_type'] == 'image') ? '图片' : '文件';

        switch($result){
            case UPLOAD_ERR_OK :
                return '';
            case UPLOAD_ERR_INI_SIZE :
            case UPLOAD_ERR_FORM_SIZE :
            case UPLOAD_ERR_FILE_SIZE :
                return $type . '大小必须在 ' .convert_size($option['max_length']). ' 以内';
            case UPLOAD_ERR_FILE_TYPE :
            case UPLOAD_ERR_IMAGE_TYPE :
                return $type . '类型错误，只能为 ' .(($option['allow_type']=='image') ? 'png/jpg/gif' : str_replace('|', '/', $option['allow_type'])). ' 格式！';
            case UPLOAD_ERR_IMAGE_SMALL :
                return '图片尺寸太小，请保证不小于 ' .$option['image_min_width']. 'x' .$option['image_min_height']. '';
            case UPLOAD_ERR_IMAGE_LARGE :
                return '图片尺寸太大，请保证不大于 ' .$option['image_max_width']. 'x' .$option['image_max_height']. '';
            case UPLOAD_ERR_FILE_IO:
                return '服务器存储失败，请重试！';
            case UPLOAD_ERR_PARTIAL :
            case UPLOAD_ERR_NO_FILE :
            case UPLOAD_ERR_NO_TMP_DIR :
            case UPLOAD_ERR_CANT_WRITE :
            case UPLOAD_ERR_EXTENSION :
                return $type . '上传错误，请重试(' .$result. ')！';
            case UPLOAD_ERR_UNKNOWN :
            default:
                return $type . '上传错误，请重试！';
        }
    }

    function __destruct(){}
}
?>